<template>
  <el-dialog :title="title" :visible="showEquipmentType" @close="btnCancel">
    <el-form ref="typeData" :model="data" :rules="rules" label-width="120px">
      <el-form-item label="型号名称：" prop="name">
        <el-input v-model="data.name" placeholder="请输入" show-word-limit maxlength="10" style="width:80%" />
      </el-form-item>
      <el-form-item label="型号编码：" prop="model">
        <el-input v-model="data.model" placeholder="请输入（限制数字、字母、中划线、下划线）" show-word-limit maxlength="15" style="width:80%" />
      </el-form-item>
      <el-form-item label="货道行数：" prop="vmRow">
        <el-input-number v-model="data.vmRow" placeholder="请输入" controls-position="right" :min="1" :max="10" style="width:80%" />
      </el-form-item>
      <el-form-item label="货道列数：" prop="vmCol">
        <el-input-number v-model="data.vmCol" placeholder="请输入" controls-position="right" :min="1" :max="10" style="width:80%" />
      </el-form-item>
      <el-form-item label="货道容量：" prop="channelMaxCapacity">
        <el-input-number v-model="data.channelMaxCapacity" placeholder="请输入" controls-position="right" :min="1" :max="10" style="width:80%" />
      </el-form-item>
      <el-form-item label="设备图片：" prop="image">
        <el-upload
          class="avatar-uploader"
          accept="image/jpeg,image/png"
          action="http://likede2-admin.itheima.net/likede/api/vm-service/sku/fileUpload"
          name="fileName"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="data.image" :src="data.image" class="avatar">
          <i v-else class="el-icon-upload2 avatar-uploader-icon" />
        </el-upload>
        <div class="el-upload__tip">支持扩展名：jpg、png，文件不得大于100kb</div>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button @click="btnCancel">取消</el-button>
          <el-button type="warning" @click="btnOK">确认</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addequipmentType, editequipmentType } from '@/api/equipment'
export default {
  props: {
    showEquipmentType: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      required: true
    },
    data: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formData: this.data,
      rules: {
        name: [{ required: true, message: '请输入' }],
        model: [{ required: true, message: '请输入' }, { pattern: /^[-_A-Za-z0-9]+$/, message: '请输入合法的型号编码', trigger: 'blur' }],
        vmRow: [{ required: true, message: '请输入' }],
        vmCol: [{ required: true, message: '请输入' }],
        channelMaxCapacity: [{ required: true, message: '请输入' }],
        image: [{ required: true, message: '请上传' }]
      }
    }
  },
  methods: {
    async btnOK() {
      try {
        await this.$refs.typeData.validate()
        if (this.formData.typeId) {
          await editequipmentType(this.formData.typeId, this.formData)
        } else {
          await addequipmentType(this.formData)
        }
        this.$emit('update:showEquipmentType', false)
        this.$emit('updateList')
        this.$refs.typeData.resetFields()
        this.formData = {
          name: '',
          model: '',
          vmRow: undefined,
          vmCol: undefined,
          channelMaxCapacity: undefined,
          image: ''
        }
      } catch (error) {
        this.$message.error(error.response.data)
      }
    },
    btnCancel() {
      this.formData = {
        name: '',
        model: '',
        vmRow: undefined,
        vmCol: undefined,
        channelMaxCapacity: undefined,
        image: ''
      }
      this.$emit('reset', this.formData)
      this.$emit('update:showEquipmentType', false)
      this.$refs.typeData.resetFields()
    },
    handleAvatarSuccess(res, fileName) {
      this.formData.image = URL.createObjectURL(fileName.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG || isPNG && isLt2M
    }
  }

}
</script>

<style>
.el-form-item{
  margin-bottom: 0;
}
.el-dialog__body{
  padding: 20px 20px 30px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 76px;
    height: 76px;
    line-height: 76px;
    text-align: center;
  }
  .avatar {
    width: 76px;
    height: 76px;
    display: block;
  }
  .el-upload{
    background: #f3f6fb;
  }
  .el-upload__tip{
    font-size: 14px;
    color: #bac0cd;
    line-height: 1;
  }
  .el-form-item__error{
    top: 73%;
    left: 17px;
  }
</style>
